<?php
include '../../../common.inc.php';
include 'fun.php';
$rootFolder = __DIR__ . '/dump';
$areas = array();
foreach ($AREA as $area) {
	if ($area['parentid'] == 0) {
		array_push($areas, $area);
	}
}

$start = isset($start) ? $start : 0;
$area = $areas[$start];

$resFolder =  $rootFolder.'/'.$area['areaname'];
$folder = iconv(DT_CHARSET, 'GBK', $rootFolder.'/'.$area['areaname']);
if (!is_dir($folder)) {
	mkdir($folder, 0777, true);
}
$childs = explode(',', $area['arrchildid']);

for($i=1;$i<=count($childs);$i++) {
	if (count(explode(',',$AREA[$childs[$i]]['arrparentid'])) == 2) {


	$file = iconv(DT_CHARSET, 'GBK', $resFolder . '/'. $AREA[$childs[$i]]['areaname'].'.csv');
	$sql = "select m.userid, m.username, c.company, m.email,m.truename, m.mobile,c.telephone,c.address, c.areaid
			from xd_member m, xd_company c
			where m.userid=c.userid and c.areaid in ({$AREA[$childs[$i]]['arrchildid']})
			order by m.userid ASC";

	$members = $db->getAll($sql);

	$str = "用户ID, 用户名, 公司名称, Email, 真实姓名, 手机, 电话, 地址, 城市id, 城市\n";

	foreach($members as $m) {
		$str .= implode(',', $m).",{$AREA[$m['areaid']]['areaname']}\n";
	}
	file_put_contents($file, iconv(DT_CHARSET, 'GBK',$str));

	} else {
		continue;
	}
}
$start +=1;
echo '<meta http-equiv="refresh" content="3; url='.$_SERVER['PHP_SELF'].'?start='.$start.'" />';
echo '<pre style="font-size:100px;">';
echo $area['areaname'];
